<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/6/16
 * Time: 21:00
 */
namespace Pay\Controller;

use Common\Common\Api\Order;
use Common\Common\Api\Ordermember;
use Common\Common\Api\Redis\Channel;
use Common\Common\Api\Redis\Product;
use Common\Common\Api\Redis\Websiteconfig;

/**
 * Class ManyiController
 * @package Pay\Controller
 * 新阳光
 */
class Yangguang2Controller extends PayController{
    protected $config;
    protected $class;
    public function __construct(){
        parent::__construct();
        $classes = explode("\\",__CLASS__);
        $class = end($classes);
        $this->class = substr($class, 0, stripos($class, "Controller"));
    }

    public function pay($array){
        $this->config = Channel::getInstance()->getById($array['channel']);

        if(!$this->config){
            $this->showmessage('通道错误!',['code'=>C("THIRDCODES.".$this->class)]);
        }

        $parameter = array(
            'code' => $this->config['code'], // 通道名称
            'title' => $this->config['title'],
            'exchange' => 1, // 金额比例
            'gateway' => $this->config['gateway'],
            'orderid' => createOrderId(),
            'out_trade_id' => I("request.ordNo") ? I("request.ordNo") : I("request.pay_orderid"),
            'body' => 'pay',
            'channel'=>$array
        );

        $isMobile = isMobile();

        $return = $this->orderadd( $parameter );

        switch($this->config['paytype']){
            case 1:
            case 2:
                $pay_bankcode = 3;
                break;
            case 3:
            case 4:
                $pay_bankcode = 2;
                break;
            default:
                $this->showmessage('此通道暂不支持此产品!',['code'=>C("THIRDCODES.".$this->class)]);
        }

        $data = [];
        $data['userName'] = $this->config['mch_id'];
        $data['tradeMoney'] = number_format($return['amount'],2,'.','');

        $data['payType'] = $pay_bankcode; //支付类型(1银行转账,2支付宝,3微信支付)
        $data['coinCode'] = 'UFC';//塞班币
        $data['orderNum'] = $return['orderid'];
        $data['notifyUrl'] =  $return['notifyurl'];

        $md5str =httpstr($data)."&ACCESS_KEY={$this->config['signkey']}";
        $md5str = strtoupper($md5str);

        $data['sign'] = md5($md5str);
        $res = curl($this->config['gateway'], httpstr($data));
        $resp = json_decode($res,true);

        if($resp['success'] !== true) {
            if ($resp['msg']) {
                $this->showmessage($resp['msg'], ['code' => C("THIRDCODES." . $this->class)]);
            }

            $this->showmessage('获取支付连接失败！',['code'=>C("THIRDCODES.".$this->class)]);
        }

        //告诉交易所此笔订单已经支付
        $url2 = 'http://www.sbbei.com/app/otc/api/otcCompletePayment';
        $data1 = [];
        $data1['userName'] = $this->config['mch_id'];
        $data1['tradeNum'] = $resp['obj']['tradeNum'];
        $data1['selPay'] =  $pay_bankcode;
        $data1['orderMarkByMoney'] = $resp['obj']['orderMarkByMoney'];

        $md5str1 =httpstr($data1)."&ACCESS_KEY={$this->config['signkey']}";
        $md5str1 = strtoupper($md5str1);
        $data1['sign'] = md5($md5str1);

        $res1 = curl($url2, httpstr($data1));

        $code = $resp['obj']['payment']['thingUrl'];
        $return['amount'] = $resp['obj']['orderMarkByMoney'];

        $paytype = transArr( C("PAYTYPES"), "id");
        $this->assign("wap_url", $code);
        $this->assign("code_url", $code);
        $this->assign("title", $paytype[$this->config['paytype']]);
        $this->assign("return", $return);
        // $this->assign("imgurl", C("website.DOMAIN")."/Pay_Qrcode_getCode?type=2&data=" . base64_encode($code));
        //选择支付页面
        switch($this->config['paytype']){
            //微信
            case 1:
            case 2:
                if($isMobile){
                    $this->assign("deny_layer", 1);
                    $this->display("WeiXin/weixinwap");
                }else{
                    $this->display("WeiXin/weixin");
                }
                break;
            //支付宝扫码
            case 3:
                //支付宝H5
            case 4:
            case 17:
            case 18:
                if($isMobile){
                    $this->assign("deny_layer", 1);
                    $this->display("WeiXin/alipaywap");
                }else{
                    $this->display("WeiXin/alipay");
                }
                break;
            default:
                $this->display("WeiXin/Pay");//收银台
                break;
        }
    }

    //通知
    public function notifyurl(){
        //$this->debug_callback();

        //IP白名单
        if(!$this->checkip(getIP(),['47.254.232.56'])){
            exit("wrong");
        }

        /*
         *
         array ( 'tradeMoney' => '100.0000000000', 'amount' => '100.0900000000', 'transactionNum' => '001906161608387637449', 'sign' => '2099ca2cd0095e54c0485bb03538974b', 'orderNum' => '2019061616084849948', 'userName' => '16978981345', 'status' => '14', )
         */

        $p = I("get.");

        if(!$p || empty($p)){
            exit("wrong");
        }

        //获取订单信息
        $orderid = $p['orderNum'];

        //获取订单
        $find = $this->checkOrderNotify($orderid);
        if(!$find){
            echo 'order_not_found'; exit;
        }

        //保存回调数据

        if($p['status'] === '14'){
            M("Order")->where(array("pay_orderid"=>$orderid))->save(array(

                "pay_orderid_rd"=>htmlspecialchars($p['transactionNum']),
                "pay_true"=>htmlspecialchars($p['amount']),
            ));
        }else{
            M("Order")->where(array("pay_orderid"=>$orderid))->save(array(
                "pay_orderid_rd"=>htmlspecialchars($p['transactionNum']),
            ));
        }

        //获取渠道信息
        $this->config = M("Channel")->where(array('code'=>$this->class))->find();

        $sign = $p['sign'];


        $data1['userName'] = $p['userName'];
        $data1['transactionNum'] = $p['transactionNum'];
        $data1['orderNum'] = $p['orderNum'];
        $data1['amount'] = $p['amount'];
        $data1['tradeMoney'] = $p['tradeMoney'];
        $data1['status'] = $p['status'];


        $md5str =httpstr($data1);
        $md5str = strtoupper($md5str);

        $sign2  = md5($md5str);

        if($sign !== $sign2){
            exit("sign_wrong");
        }

        if($p['status'] !== '14'){
            exit("pay_fail");
        }

        $this->async($find['pay_orderid'],"");
    }

    public function callBackUrl(){

        echo '处理成功'; exit;
    }
}